<template>
  <div class="product-detail">
    <van-swipe class="my-swipe" :autoplay="1000" indicator-color="white">
      <van-swipe-item v-for="item in gallery" :key="item.id">
        <img :src="item.img_url" width="100%" alt="" style="display: block" />
      </van-swipe-item>
    </van-swipe>
    <Tips></Tips>

    <!--  -->
    <div class="info">
      <div class="title">{{ info.name }}</div>
      <div class="goods_brief">{{ info.goods_brief }}</div>
      <div class="retail_price">{{ info.retail_price | RMBformat }}</div>
    </div>

    <!--  -->
    <div class="attribute">
      <h3>商品参数</h3>
      <ul>
        <li v-for="(item, key) in attribute" :key="key">
          <div class="l">{{ item.name }}</div>
          <div class="r van-ellipsis">{{ item.value }}</div>
        </li>
      </ul>
    </div>

    <!-- 大量图片信息 -->
    <div class="goods_desc" v-html="goods_desc"></div>

    <!--  -->
    <div class="title0">
      <span>大家都在看</span>
    </div>
    <Products :goodsList="goodsList" />

    <!-- SKU -->
    <van-sku
      ref="sku"
      v-model="isSkuShow"
      :sku="sku"
      :goods="goods"
      :hide-stock="sku.hide_stock"
    />

    <!-- AppGoodActive -->
    <AppGoodActive @addToCart="addToCart"></AppGoodActive>
  </div>
</template>

<script>
import { GetGoodsDetailData, GetGoodsRelatedData } from "@/request/api";
import Tips from "@/components/Tips";
import Products from "@/components/Products";
import AppGoodActive from "@/components/AppGoodActive";

export default {
  name: "ProductDetail",
  components: { Tips, Products, AppGoodActive },
  data() {
    return {
      // sku相关数据：
      isSkuShow: false,
      sku: {
        tree: [],
        hide_stock: false,
        price: "0",
        stock_num: 0,
      },
      goods: {
        picture: "",
      },
      // ---------------------------

      //轮播图数据
      gallery: [],
      // 商品信息
      info: {},
      // 商品参数
      attribute: [],
      // 商品信息(大量图片信息)
      goods_desc: "",
      // 相关产品列表
      goodsList: [],
    };
  },
  created() {
    //发送请求，获得详细信息
    GetGoodsDetailData({
      id: this.$route.query.id,
    }).then((res) => {
      // sku
      this.goods.picture = res.data.info.list_pic_url;
      this.sku.price = res.data.info.retail_price;
      this.sku.stock_num = res.data.info.goods_number;
      //
      console.log(res.data);
      this.gallery = res.data.gallery;
      this.info = res.data.info;

      this.attribute = res.data.attribute;

      this.goods_desc = res.data.info.goods_desc;
    });

    // 发送请求获取相关产品数据(大家都在看模块)
    GetGoodsRelatedData({
      id: this.$route.query.id,
    }).then((res) => {
      console.log(res.data);
      this.goodsList = res.data.goodsList;
    });
  },
  methods: {
    addToCart() {
      // 点击购物车按钮
      if (this.isSkuShow) {
        // 加入购物车
      } else {
        //
        this.isSkuShow = true;
      }
    },
  },
};
</script>

<style lang="less">
.product-detail {
  margin-bottom: 0.5rem;

  .info {
    background-color: #fff;
    text-align: center;
    line-height: 0.4rem;
    padding: 0.1rem 0;
    border-bottom: 1px solid #ccc;
    .title {
      font-size: 0.2rem;
    }
    .retail_price {
      color: darkred;
    }
  }

  .attribute {
    background-color: #fff;
    margin-top: 0.2rem;
    padding: 0.1rem 0.168rem;
    h3 {
      font-size: 0.1638rem;
      line-height: 0.5rem;
    }
    li {
      height: 0.4rem;
      line-height: 0.4rem;
      background-color: #efefef;
      display: flex;
      margin-bottom: 0.1rem;
      border-radius: 0.05rem;
      .l {
        width: 30%;
        text-align: right;
        margin-right: 0.1rem;
        font-size: 0.12rem;
      }
      .r {
        flex: 1;
        font-size: 0.168rem;
      }
    }
  }

  .goods_desc {
    img {
      display: block;
      width: 100%;
    }
  }
}
</style>